package com.compass.service;

import com.compass.constant.Admin;
import com.compass.constant.ProtocolType;
import com.compass.entity.*;
import com.compass.mapper.BaseProtocolMapper;
import com.compass.mapper.DubboProtocolMapper;
import com.compass.mapper.HttpProtocolMapper;
import com.compass.mapper.TestCaseMapper;
import com.compass.util.SessionUtil;
import com.compass.vo.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/4/21
 * Time: 10:39
 * PROJECT：compass
 * PACKAGE: com.compass
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Service
@Slf4j
public class TestCaseService {

    @Autowired
    ComponentService componentService;
    @Autowired
    DubboProtocolMapper dubboProtocolMapper;

    @Autowired
    TestCaseMapper testCaseMapper;


    /**
     * 获取项目列表，前提是有权限,当你是项目成员的时候你是可以看见项目列表
     **/
    public ResponseMessage getTestCaseList(TestCaseDo testCaseDo) {
        try {
            Page page = PageHelper.startPage(testCaseDo.getPageNum(), testCaseDo.getPageSize(), true);
            List<TestCaseDo> testCaseList = testCaseMapper.getTestCaseList(testCaseDo);
            Long total = page.getTotal();
            TablePage tablePage = new TablePage();
            tablePage.setRows(testCaseList);
            tablePage.setTotal(total);
            return ResponseMessage.successResponse(tablePage);
        } catch (Exception e) {
            log.error("查询项目异常", e);
        }
        return ResponseMessage.successResponse();
    }
    /**
     * 获取dubbo组件和webService组件的入参模板
     **/
    public String getParamModel(Integer interfaceId,String interfaceType) {
//        componentService.getComponentById(interfaceId);
        String paramModel=null;
        if(interfaceType.equalsIgnoreCase(ProtocolType.DUBBO)) {
            BaseProtocolDo baseProtocolDo = dubboProtocolMapper.getComponentById(interfaceId);
            paramModel = ((DubboDo) baseProtocolDo).getParam();
        }else if(interfaceType.equalsIgnoreCase(ProtocolType.WEBSERVICE)){

        }
        return paramModel;
    }

    /**
     * 新增用例
     * @param
     * @return
     */
    public ResponseMessage addTestCase(TestCaseDo testCaseDo) {
        try {
            Integer id = testCaseDo.getId();
            int i =0;
            if (null != id) {
                 i = testCaseMapper.modifyTestCase(testCaseDo);
            }else {
                 i = testCaseMapper.addTestCase(testCaseDo);
            }
            if (1 != i) {
                return ResponseMessage.errorResponse("添加测试用例异常," + Admin.ADMIN_MESSAGE);
            }
            return ResponseMessage.successResponse();
        } catch (Exception e) {
            log.error("添加测试用例异常", e);
            return ResponseMessage.errorResponse("添加测试用例异常," + Admin.ADMIN_MESSAGE);
        }
    }

}
